package cn.pzhu.controller.web;

import cn.pzhu.constant.ConstantLogin;
import cn.pzhu.controller.BaseController;
import cn.pzhu.services.web.AccountServices;

import javax.servlet.http.HttpSession;

/**
 * Created by dongp on 2016/5/6.
 */
public class AccountController extends BaseController {

    private AccountServices loginServices = new AccountServices();

    public void login() {
        String account = getPara("account");
        String password = getPara("password");
        String vCode = getPara("vCode","");
        HttpSession session = getSession();
        if(!vCode.equals(session.getAttribute("vCode"))) {
            setAttr("msg", "验证错误");
            render("login/login.jsp");
            return;
        }

        int result = loginServices.login(account,password,getRequest());
        if(result == ConstantLogin.login_info_1){
            this.redirect("/");// 首页
        }else{
            // 返回错误信息
            this.render("login/login.jsp");
        }
    }

    public void registry() {
        String account = getPara("account");
        String password = getPara("password");
        String vCode = getPara("vCode","");
        HttpSession session = getSession();
        if(!vCode.equals(session.getAttribute("vCode"))) {
            setAttr("msg", "验证错误");
            render("login/registry.jsp");
            return;
        }

        boolean result = loginServices.registry(account,password,getRequest());
        if(result){
            this.render("login/success.jsp");// 注册成功页面
        }else{
            // 返回错误信息
            this.render("login/registry.jsp");
        }
    }
}
